//
//  RHFbQuestionTypeCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/11/28.
//

#import "RHFbQuestionTypeCell.h"
@interface RHFbQuestionTypeCell()

@property (nonatomic ,assign) NSInteger selectIdx;

@property (nonatomic ,strong) NSArray *typeArr;
@end
@implementation RHFbQuestionTypeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}
-(void)setKey:(NSString *)key
{
    [super setKey:key];
    if(self.selectIdx == -1){
        self.selectIdx = 0;
        [self.para setObject:self.typeArr[0] forKey:key];
    }
}

-(void)setupUI{
    kWeakSelf;
    self.selectIdx = -1;
    
    self.contentView.backgroundColor = CFTabViewBgColor;
    
    UIView *containView = kView;
    containView.backgroundColor = [UIColor whiteColor];
    [containView setMiddleBorder];
    [self.contentView addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.bottom.mas_offset(0);
    }];
    
   
    
    __block UIView *lastView;
    CGFloat item_width = (kUI_WINDOW_W - 15*4-8*2)/3;
    
    self.typeArr = @[@"EMS",@"Master",@"PCS",@"PACK",@"Inverter"];
    [self.typeArr enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        QMUIButton *btn = [[QMUIButton alloc]init];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
        [btn setBackgroundColor:CFTabViewBgColor];
        [btn setCornerRadius:8];
        [containView addSubview:btn];
        
        QMUILabel *contentLab = [[QMUILabel alloc]init];
        contentLab.text = obj;
        contentLab.font = [UIFont systemFontOfSize:14];
        contentLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:0.8];
        contentLab.textAlignment = NSTextAlignmentCenter;
        contentLab.numberOfLines = 0;
        [btn addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(5);
            make.right.mas_offset(-5);
            make.centerY.mas_offset(0);
        }];
        
        if(idx == 0){
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.top.mas_offset(5);
                make.width.offset(item_width);
                make.height.offset(kScaleW(40));
            }];
        }else{
            if(idx%3 == 0){
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(15);
                    make.top.equalTo(lastView.mas_bottom).offset(8);
                    make.size.equalTo(lastView);
                }];
                
            }else{
                [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(8);
                    make.top.equalTo(lastView);
                    make.size.equalTo(lastView);
                }];
            }
        }
        
        lastView = btn;
       
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf btnAction:idx];
            [weakSelf.para setObject:obj forKey:weakSelf.key];
        }];
        
        
        [RACObserve(weakSelf, selectIdx) subscribeNext:^(id  _Nullable x) {
            NSInteger index = [x integerValue];
            
            if (index == idx) {
                [btn setBackgroundColor:CMainColorGreen];
                [contentLab setTextColor:UIColor.whiteColor];
                [btn rounded:8 width:1 color:CMainColorGreen];
            }else{
                [btn setBackgroundColor:CFTabViewBgColor];
                [contentLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
                [btn rounded:8 width:1 color:kBorderColor];
            }
            
        }];
    }];
    
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-8);
    }];
    
    
}

-(void)btnAction:(NSInteger)idx
{
    self.selectIdx = idx;
}

@end
